<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>登录</h1>
      <el-card shadow="never" class="login-card">
        <!--登录表单-->
        <el-form ref="formRef" :model="loginForm" :rules="rules">
          <el-form-item prop="mobile">
            <el-input v-model="loginForm.mobile" placeholder="请输入手机号" />
          </el-form-item>
          <el-form-item prop="password" style="position: relative;">
            <el-input v-model="loginForm.password" placeholder="请输入密码" :type="isShow ? 'text' : 'password'" />
            <span style="position: absolute; left:320px; cursor:pointer">
              <svg-icon :icon-class="isShow ? 'eye-open' : 'eye'" @click="isShow = !isShow" />
            </span>
          </el-form-item>
          <el-form-item prop="isAgree">
            <el-checkbox v-model="loginForm.isAgree">用户平台使用协议</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="isLoading" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Login',
  data() {
    // rule  表示当前字段的信息
    // value 表示关联的字段
    // callback 是一个放行的函数 callback()表示方向通过  callback(new Error('请勾选用户协议')) 表示没有勾选提示
    const validatePass = (rule, value, callback) => {
      value ? callback() : callback(new Error('请勾选用户协议'))
    }
    return {
      isLoading: false,
      isShow: false,
      // 登录表单关联的字段
      loginForm: {
        mobile: '13800000002',
        password: 'hm#qd@23!',
        isAgree: true
      },
      // 登录表单校验规则
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '手机号输入不正确', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: ['blur', 'change'] },
          { min: 6, max: 12, message: '密码长度需要在6-12位之间', trigger: ['blur', 'change'] }
        ],
        isAgree: [
          { validator: validatePass, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.formRef.validate(async too => {
        // console.log(too) // 布尔类型的值
        if (!too) return
        // 设置按钮的等待状态，避免网络过慢时，用户重复点击，重复发请求
        this.isLoading = true
        // 发送请求
        // axios.post('https://heimahr.itheima.net/api/sys/login', this.loginForm)
        // axios.post('http://localhost:9528/api/sys/login', this.loginForm)
        // 请求在vuex中发送，由于是异步操作，在actions中发送 点击触发actions的事件
        // request.post('/sys/login', this.loginForm) // 不写根地址，会自动拼接启动服务的地址
        // console.log('发送请求')
        // vuex中actions中的所有的函数，都是promise对象，由于是异步，需要等待登录成功之后在进行跳转
        try {
          await this.$store.dispatch('user/login', this.loginForm)
          this.$router.push({
            path: '/',
            query: { id: 15 }
          })
          // 登录成功之后按钮回复
        } finally {
          // 不管错误都会执行
          this.isLoading = false
        }
      })
    }
  }

}
</script>
<style lang="scss">
.login-container {

  display: flex;
  align-items: stretch;
  height: 100vh;

  .logo {
    flex: 3;
    background: rgba(38, 72, 176) url(../../assets/common/login_back.png) no-repeat center / cover;
    border-top-right-radius: 60px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    padding: 0 100px;

    .icon {
      background: url(../../assets/common/logo.png) no-repeat 70px center / contain;
      width: 300px;
      height: 50px;
      margin-bottom: 50px;
    }

    p {
      color: #fff;
      font-size: 18px;
      margin-top: 20px;
      width: 300px;
      text-align: center;
    }
  }

  .form {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 176px;

    .el-card {
      border: none;
      padding: 0;
    }

    h1 {
      padding-left: 20px;
      font-size: 24px;
    }

    .el-input {
      width: 350px;
      height: 44px;

      .el-input__inner {
        background: #f4f5fb;
      }
    }

    .el-checkbox {
      color: #606266;
    }

    .el-button {
      width: 350px;
    }
  }
}
</style>
